package com.hemaapp.yjnh.adapter;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import xtom.frame.image.load.XtomImageTask;
import xtom.frame.image.load.XtomImageTask.Size;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.util.XtomWindowSize;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.hemaapp.hm_FrameWork.view.photoview.PhotoView;
import com.hemaapp.hm_FrameWork.view.photoview.PhotoViewAttacher.OnViewTapListener;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShowLargeImageActivity;

/**
 *
 */
public class PhotoPagerAdapter extends PagerAdapter {
	private BaseActivity activity;
	private ArrayList<String> urllist;

	public PhotoPagerAdapter(BaseActivity context, ArrayList<String> urllist) {
		this.activity = context;
		this.urllist = urllist;
	}

	@Override
	public int getCount() {
		return urllist == null ? 0 : urllist.size();
	}

	@Override
	public View instantiateItem(ViewGroup container, int position) {
		PhotoView photoView = new PhotoView(container.getContext());
		photoView.setOnViewTapListener(new OnViewTapListener() {

			@Override
			public void onViewTap(View view, float x, float y) {
				ShowLargeImageActivity a = (ShowLargeImageActivity) activity;
//				a.toogleTitle();
			}
		});
		photoView.setOnViewTapListener(new OnViewTapListener() {
			
			@Override
			public void onViewTap(View view, float x, float y) {
				activity.finish();
			}
		});
		// Now just add PhotoView to ViewPager and return it
		container.addView(photoView, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		try {
			URL url = new URL(urllist.get(position));
			activity.imageWorker.loadImage(new XtomImageTask(photoView, url,
					activity));
		} catch (MalformedURLException e) {
			int width = XtomWindowSize.getWidth(activity);
			activity.imageWorker.loadImage(new XtomImageTask(photoView, urllist
					.get(position), activity, new Size(width, width)));
		}
		return photoView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}
	
	private class ImageTask extends XtomImageTask {

		private ImageView imageView;
		
		public ImageTask(ImageView imageView, String path, Object context, Size size) {
			super(imageView, path, context, size);
			this.imageView = imageView;
		}
		
		public ImageTask(ImageView imageView, URL url, Object context) {
			super(imageView, url, context);
			this.imageView = imageView;
		}

		@Override
		public void success() {
			activity.cancelProgressDialog();
			super.success();
		}

		@Override
		public void failed() {
			super.failed();
			XtomToastUtil.showShortToast(activity, "加载图片出错");
			this.imageView.setImageResource(R.drawable.no_pic_big);
		}

		@Override
		public void beforeload() {
			activity.showProgressDialog("加载中", true);
			super.beforeload();
		}
		
		
	}
}
